package com.ruoyi.project.product.style.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.product.style.domain.Style;
import com.ruoyi.project.product.style.mapper.StyleMapper;
import com.ruoyi.project.product.style.service.StyleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 高柯
 * @Date: 2020/4/15
 * @Description: com.ruoyi.project.product.style.service
 * @version: 1.0
 */
@Service
public class StyleServiceImpl implements StyleService {

    @Autowired
    private StyleMapper styleMapper;

    @Override
    public List<Style> selectStyleList(Style style) {
        return styleMapper.selectStyleList(style);
    }

    @Override
    public String checkStyleNameUnique(Style style) {
        Long styleId = StringUtils.isNull(style.getStyleId()) ? -1L : style.getStyleId();
        Style info = styleMapper.checkStyleNameUnique(style.getStyleName());
        if (StringUtils.isNotNull(info) && info.getStyleId().longValue() != styleId.longValue())
        {
            return UserConstants.SERIES_NAME_NOT_UNIQUE;
        }
        return UserConstants.SERIES_NAME_UNIQUE;
    }

    @Override
    public int insertStyle(Style style) {
        style.setCreateBy(ShiroUtils.getLoginName());
        return styleMapper.insertStyle(style);
    }

    @Override
    public Style selectStyleById(Long styleId) {
        return styleMapper.selectStyleById(styleId);
    }

    @Override
    public int updateStyle(Style style) {
        return styleMapper.updateStyle(style);
    }

    @Override
    public int deleteStyleByIds(String ids) {
        Long[] styleIds = Convert.toLongArray(ids);
        //应该在删除之前先校验该类型下是否有公告，有则不能删除，报错

        return styleMapper.deleteStyleByIds(styleIds);
    }
}
